import { DateRules } from './dateRule';
let _DateRules = DateRules;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import DateRuleMock from './mk/mng/card/dateRuleMock';
 * DateRuleMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mk/mng/card/dateRule/listKey').reply(config => {
      let params = config.params;
      let mockDateRules = _DateRules.filter(dateRule => {
      	if(params.type||params.type==''){ if( dateRule.type.indexOf(params.type)>=0) return true;}
      	if(params.beginTime||params.beginTime==''){ if( dateRule.beginTime.indexOf(params.beginTime)>=0) return true;}
      	if(params.endTime||params.endTime==''){ if( dateRule.endTime.indexOf(params.endTime)>=0) return true;}
      	if(params.fixedTerm||params.fixedTerm==''){ if( dateRule.fixedTerm.indexOf(params.fixedTerm)>=0) return true;}
      	if(params.fixedBeginTerm||params.fixedBeginTerm==''){ if( dateRule.fixedBeginTerm.indexOf(params.fixedBeginTerm)>=0) return true;}
      	if(params.cardPkId||params.cardPkId==''){ if( dateRule.cardPkId.indexOf(params.cardPkId)>=0) return true;}
      	if(params.id||params.id==''){ if( dateRule.id.indexOf(params.id)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockDateRules
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mk/mng/card/dateRule/list').reply(config => {
      let params = config.params;
      let mockDateRules = _DateRules.filter(dateRule => {
      	var isTrue=false;
      	if(params.type||params.type==''){if(dateRule.type.indexOf(params.type)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.beginTime||params.beginTime==''){if(dateRule.beginTime.indexOf(params.beginTime)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.endTime||params.endTime==''){if(dateRule.endTime.indexOf(params.endTime)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.fixedTerm||params.fixedTerm==''){if(dateRule.fixedTerm.indexOf(params.fixedTerm)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.fixedBeginTerm||params.fixedBeginTerm==''){if(dateRule.fixedBeginTerm.indexOf(params.fixedBeginTerm)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.cardPkId||params.cardPkId==''){if(dateRule.cardPkId.indexOf(params.cardPkId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.id||params.id==''){if(dateRule.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockDateRules
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockDateRules.length;
      mockDateRules = mockDateRules.filter((dateRule, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockDateRules,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mk/mng/card/dateRule/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _DateRules = _DateRules.filter(dateRule => dateRule.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mk/mng/card/dateRule/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _DateRules = _DateRules.filter(dateRule => !params.includes(dateRule.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mk/mng/card/dateRule/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { type,beginTime,endTime,fixedTerm,fixedBeginTerm,cardPkId,id } = params;
      _DateRules.some(dateRule => {
        if (dateRule.id === id) {
			dateRule.type = type;
			dateRule.beginTime = beginTime;
			dateRule.endTime = endTime;
			dateRule.fixedTerm = fixedTerm;
			dateRule.fixedBeginTerm = fixedBeginTerm;
			dateRule.cardPkId = cardPkId;
			dateRule.id = id
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mk/mng/card/dateRule/add').reply(config => {
      var params = JSON.parse( config.data );
      let { type,beginTime,endTime,fixedTerm,fixedBeginTerm,cardPkId,id } = params;
      _DateRules.push({
		type:type,
		beginTime:beginTime,
		endTime:endTime,
		fixedTerm:fixedTerm,
		fixedBeginTerm:fixedBeginTerm,
		cardPkId:cardPkId,
		id:id
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};